//
//  FTValueHolder.m
//  WebPDA
//
//  Created by Justin Yip on 8/24/10.
//  Copyright 2010 TenNights.com. All rights reserved.
//

#import "FTValueHolder.h"


@implementation FTValueHolder
@synthesize entity;
@synthesize dictionary;

-(id)initWithEntity:(id)aEntity {
	if ((self = [super init])) {
		dictionary = [[NSMutableDictionary alloc] init];//no retain here
		self.entity = aEntity;//retain here
	}
	return self;
}

-(id)valueForKey:(NSString *)key {
	if (nil != entity && [entity respondsToSelector:NSSelectorFromString(key)]) {
		return [entity valueForKey:key];
	}else {
		return [dictionary valueForKey:key];
	}
	return [super valueForKey:key];
}

-(void)setValue:(id)value forKey:(NSString *)key {
	//1.
	[dictionary setValue:value forKey:key];
	//2.
	if (nil != entity) {
		//setXxx
		NSString *selectorString = [NSString stringWithFormat:@"set%@:", [key capitalizedString]];
		SEL selector = NSSelectorFromString(selectorString);
		if ([entity respondsToSelector:selector]) {
			[entity performSelector:selector withObject:value];
		}
	}
}

-(void)dealloc {
	[entity release];
	[dictionary release];
	[super dealloc];
}
@end
